package com.zl.basis;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.zl.basis.component.AppInfo;
import com.zl.basis.component.AppUtil;
import com.zl.basis.component.ServiceUtil;
import com.zl.basis.config.BasisConfig;
import com.zl.basis.config.BasisConstant;
import com.zl.basis.exception.AppExceptionListener;
import com.zl.basis.log.KeyValuesException;
import com.zl.basis.service.TimeTaskListener;
import com.zl.basis.service.TimeTaskService;
import com.zl.basis.util.StringUtil;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import timber.log.Timber;

public class BasisManger {
    private static Context mContext;

    public static void init(Context mContext, @Nullable String configPath, @Nullable AppInfo appInfo) {
        setContext(mContext);
        if (!StringUtil.isEmpty(configPath)) {
            BasisConfig.initConfig(configPath);
        } else {
            BasisConfig.initConfig();
        }
        if (null == appInfo) {
            BasisConfig.setAppInfo(AppUtil.getAppInfo(getContext()));
        } else {
            BasisConfig.setAppInfo(appInfo);
        }
    }


    public static void initTimeTask(Context mContext, @Nullable TimeTaskListener timeTaskListener) {
        ServiceUtil.startService(mContext, BasisConstant.SERVICE_ACTION_TIME_TASK);
        if (null != timeTaskListener) {
            TimeTaskService.setTimeTaskListener(timeTaskListener);
        }
    }

    public static void setTimeTaskListener(TimeTaskListener timeTaskListener) {
        if (null != timeTaskListener) {
            TimeTaskService.setTimeTaskListener(timeTaskListener);
        }
    }

    public static void startSoundTipService(Context mContext) {
        ServiceUtil.startService(mContext, BasisConstant.SERVICE_ACTION_SOUND_TIP);
    }

    public static void initAliYunLogInfo(Context mContext,  AppExceptionListener exceptionListener) {
        Timber.plant(new Timber.Tree() {
            @Override
            protected void log(int priority, @Nullable String tag, @NotNull String message, @Nullable Throwable t) {
                if (!StringUtil.isEmpty(message) && !StringUtil.isEmpty(tag)) {
//                    AliYunLogInfo.upLoadLog(mContext, tag, message);
                }
                if (t != null && t instanceof KeyValuesException) {
//                    AliYunLogInfo.upLoadLog(mContext, ((KeyValuesException) t).getLogMaps());
                }
                if (t != null && priority == Log.ERROR) {
                    Log.i(BasisConstant.TAG, t.getMessage());
                    if (exceptionListener != null) {
                        exceptionListener.onException(t);
                    }
                }
            }
        });
    }


    public static void showShortToast(String msg) {
        showShortToast(getContext(), msg);
    }

    public static void showShortToast(Context mContext, String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

    public static void showLongToast(String msg) {
        showLongToast(getContext(), msg);
    }

    public static void showLongToast(Context mContext, String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();
    }

    public static void installApp(String apkPath) {
        Intent intent = new Intent();
        intent.setAction(BasisConstant.BROADCAST_ACTION_INSTALL_APP);
        intent.putExtra("apk_path", apkPath);
        getContext().sendBroadcast(intent);
    }

    public static void rebootDevice() {
        Intent intent = new Intent();
        intent.setAction(BasisConstant.BROADCAST_ACTION_DEVICE_REBOOT);
        if (null != getContext()) {
            getContext().sendBroadcast(intent);
        }
    }

    public static void rebootDevice(Context mContext) {
        Intent intent = new Intent();
        intent.setAction(BasisConstant.BROADCAST_ACTION_DEVICE_REBOOT);
        if (null != mContext) {
            mContext.sendBroadcast(intent);
        }
    }

    public static Context getContext() {
        return mContext;
    }

    public static void setContext(Context mContext) {
        BasisManger.mContext = mContext;
    }
}
